package com.bbb.blogservice.dao.blog;

import com.bbb.blogservice.entity.BlogContent;
import com.bbb.blogservice.entity.BlogInfo;
import com.bbb.blogservice.entity.BlogTopic;
import com.bbb.blogservice.entity.BlogVideo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository(value = "blogDao")
public class BlogDaoImpl implements BLogDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void saveTopic(BlogTopic blogTopic) {
        String sql = "insert into blog_topic (id, topic, create_time, create_user) values (?, ?,?,?)";
        Object[] objects = new Object[]{blogTopic.getId(), blogTopic.getTopic(), blogTopic.getCreateTime(), blogTopic.getCreateUser()};
        jdbcTemplate.update(sql, objects);
    }

    @Override
    public void updateTopic(BlogTopic blogTopic) {

        String sql = " UPDATE blog_topic SET topic = ? WHERE id = ? ";

        Object[] objects = new Object[] {blogTopic.getTopic(), blogTopic.getId()};

        jdbcTemplate.update(sql, objects);
    }

    @Override
    public void saveBlogInfo(BlogInfo blogInfo) {

        StringBuilder insertBlogInfo = new StringBuilder();
        insertBlogInfo.append(" INSERT INTO `blog_info` ");
        insertBlogInfo.append(" (`id`, ");
        insertBlogInfo.append(" `title`, ");
        insertBlogInfo.append(" `create_user`, ");
        insertBlogInfo.append(" `topic_id`, ");
        insertBlogInfo.append(" `create_time`) ");
        insertBlogInfo.append(" VALUES ");
        insertBlogInfo.append(" (?, ?, ?, ?, now()); ");

        Object[] objects = new Object[] {blogInfo.getId(), blogInfo.getTitle(), blogInfo.getCreateUser(),
                blogInfo.getTopicId()};

        jdbcTemplate.update(insertBlogInfo.toString(), objects);
    }

    @Override
    public void saveBlogContent(BlogContent blogContent) {
        StringBuilder insertBlogContent = new StringBuilder();
        insertBlogContent.append(" INSERT INTO `blog_content` ");
        insertBlogContent.append(" (`blog_id`, ");
        insertBlogContent.append(" `content`, ");
        insertBlogContent.append(" `create_user`, ");
        insertBlogContent.append(" `create_time`) ");
        insertBlogContent.append(" VALUES ");
        insertBlogContent.append(" (?, ?, ?, now()); ");

        Object[] objects = new Object[] {blogContent.getBlogId(), blogContent.getContent(), blogContent.getCreateUser()};

        jdbcTemplate.update(insertBlogContent.toString(), objects);
    }

    @Override
    public void updateBlogInfo(BlogInfo blogInfo) {

        String sql = " update blog_info set title = ? where id = ? ";

        Object[] objects = new Object[] {blogInfo.getTitle(), blogInfo.getId()};

        jdbcTemplate.update(sql, objects);
    }

    @Override
    public void updateBlogContent(BlogContent blogContent) {

        String sql = " update blog_content set content = ? where blog_id = ? ";

        Object[] objects = new Object[] {blogContent.getContent(), blogContent.getBlogId()};

        jdbcTemplate.update(sql, objects);
    }

    @Override
    public List<BlogTopic> getAllTopicByUser(Long userId) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT `id`, ");
        sql.append(" `topic`, ");
        sql.append(" `create_time`, ");
        sql.append(" `create_user` ");
        sql.append(" FROM `blog_topic` ");
        sql.append(" where create_user = ?; ");

        Object[] objects = new Object[]{userId};

        RowMapper<BlogTopic> rowMapper = new BeanPropertyRowMapper<>(BlogTopic.class);
        List<BlogTopic> blogTopics = jdbcTemplate.query(sql.toString(), objects, rowMapper);
        return blogTopics;
    }

    @Override
    public List<BlogInfo> getAllBlogByTopicId(String topicId) {
        StringBuilder querySql = new StringBuilder();
        querySql.append(" SELECT ");
        querySql.append(" `id`, `title`, `create_user`, `create_time`, `topic_id` ");
        querySql.append(" FROM ");
        querySql.append(" `blog_info` ");
        querySql.append(" where ");
        querySql.append(" `topic_id` = ? ");

        Object[] queryObject = new Object[]{topicId};
        RowMapper<BlogInfo> rowMapper = new BeanPropertyRowMapper<>(BlogInfo.class);
        List<BlogInfo> blogInfoList = jdbcTemplate.query(querySql.toString(), queryObject, rowMapper);
        return blogInfoList;
    }

    @Override
    public Map<String, Object> getBlogById(Long id) {
        StringBuilder querySql = new StringBuilder(" SELECT ");
        querySql.append(" bc.id, bc.blog_id, bc.content, bi.title ");
        querySql.append(" FROM ");
        querySql.append(" blog_content bc ");
        querySql.append(" LEFT JOIN ");
        querySql.append(" blog_info bi ON bi.id = bc.blog_id ");
        querySql.append(" WHERE bc.blog_id = ? ");
        querySql.append(" limit 1 ");

        Object[] queryObject = new Object[]{id};

        Map<String, Object> contentMap = jdbcTemplate.queryForMap(querySql.toString(), queryObject);
        return contentMap;
    }

    @Override
    public List<BlogInfo> getAllBlog() {

        String sql = " select id, title, create_user, create_time, topic_id from blog_info ";

        RowMapper<BlogInfo> rowMapper = new BeanPropertyRowMapper<>(BlogInfo.class);
        List<BlogInfo> blogInfoList = jdbcTemplate.query(sql.toString(), rowMapper);
        return blogInfoList;

    }

    @Override
    public List<BlogVideo> getBlogVideo(Long blogId) {

        String sql = " select id, blog_id, video_url from blog_video where blog_id = ? ";
        RowMapper<BlogVideo> rowMapper = new BeanPropertyRowMapper<>(BlogVideo.class);
        Object[] objects = new Object[]{blogId};
        List<BlogVideo> blogVideoList = jdbcTemplate.query(sql, objects, rowMapper);
        return blogVideoList;
    }


}
